﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Design
{
    public partial class BFilterPanelForm : Form
    {
        private FilterPanel _filterPanel;
        DevExpress.XtraEditors.Repository.RepositoryItemComboBox repositoryItem = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();

        public BFilterPanelForm(FilterPanel filterPanel)
        {
            InitializeComponent();

            this._filterPanel = filterPanel;
            foreach (Control control in ControlHelper.GetAllControl(filterPanel.FindForm().Controls, typeof(DataGrid)))
            {
                this.BComboBox1.Items.Add(control);
            }
            if (this.BComboBox1.Items.Count > 0)
                this.BComboBox1.SelectedValue = this.BComboBox1.Items[0];

            if (Services.FilterPanelMatchControlService != null)
            {
                repositoryItem.Items.AddRange(Services.FilterPanelMatchControlService.GetCollection());
            }
        }

        private void BComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            this.BDataGrid1.Rows.Clear();
            Marmat.Forms.DataGrid dataGrid = this.BComboBox1.SelectedValue as Marmat.Forms.DataGrid;
            if (dataGrid != null)
            {
                foreach (Marmat.Forms.Views.DataGrid.Column column in dataGrid.Columns)
                {
                    Marmat.Forms.Views.DataGrid.Row row = this.BDataGrid1.AddNew();
                    row["选择"] = false;
                    row["字段"] = column.Name;
                    row["标题"] = column.Caption;
                    row["数据类型"] = column.DataType;
                    row["控件类型"] = null;
                }
            }
            this.BDataGrid1.AcceptChanges();
        }

        private void BDataGrid1_CustomRowCellEditForEditing(object sender, Marmat.Forms. Views.DataGrid.CustomRowCellEditEventArgs e)
        {
            switch (e.Column.Name)
            {
                case "控件类型":
                    e.RepositoryItem = repositoryItem;
                    break;
                default:
                    break;
            }
        }

        private void BButton1_Click(object sender, EventArgs e)
        {
            if (Services.FilterPanelMatchControlService != null)
            {
                foreach (Marmat.Forms.Views.DataGrid.Row row in this.BDataGrid1.Rows)
                {
                    if (true.Equals(row["选择"]))
                    {
                        SmartMatchControlInfo info = new SmartMatchControlInfo()
                        {
                            PropertyName = (row["标题"] ?? row["字段"]).ToString(),
                            PropertyType = row["数据类型"] as Type
                        };
                        row["控件类型"] = Services.FilterPanelMatchControlService.Match(info);
                    }
                    else
                    {
                        row["控件类型"] = null;
                    }
                }
            }
        }
    }
}
